package com.azkj.album.view.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.azkj.album.R;
import com.azkj.album.adapter.HistotyAdapter;
import com.azkj.album.adapter.VipCardAdapter;
import com.azkj.album.dto.HistoryBean;
import com.azkj.album.dto.LoginBean;
import com.azkj.album.dto.VipCardBean;
import com.azkj.album.dto.VipInfoBean;
import com.azkj.album.network.ConfigStorage;
import com.azkj.album.network.utils.ToastUtils;
import com.azkj.album.presenter.GetVIPInfoPresenter;
import com.azkj.album.utils.StatusBarUtil;
import com.azkj.album.view.base.BasePayActivity;
import com.azkj.album.view.iview.IVipView;
import com.azkj.album.view.widgets.dialog.BottomPayDialog;
import com.azkj.album.view.widgets.staggedrv.GridItemDecoration;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MemberActivity extends BasePayActivity implements IVipView {

    @BindView(R.id.tv_name)
    TextView mTvName;

    @BindView(R.id.tv_phone)
    TextView mTvPhone;

    @BindView(R.id.tv_vip_info)
    TextView mTvVipInfo;

    @BindView(R.id.tv_vip_role)
    WebView mTvVipRole;

    @BindView(R.id.rv_vip_list)
    RecyclerView mRvVipList;
    @BindView(R.id.rv_history_list)
    RecyclerView mRvHistoryList;

    @BindView(R.id.tv_pay)
    TextView tvPay;

    private GetVIPInfoPresenter mPresenter;

    private VipCardAdapter vipCardAdapter;
    private HistotyAdapter histotyAdapter;
    VipCardBean.CardDTO cardDTO;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.immersive(this);
    }


    @Override
    public void initView() {
        //支付卡
        vipCardAdapter = new VipCardAdapter(R.layout.layout_vip_add_item);
        mRvVipList.setLayoutManager(new GridLayoutManager(this, 3));
        mRvVipList.addItemDecoration(new GridItemDecoration(this, 10));
        mRvVipList.setAdapter(vipCardAdapter);
        vipCardAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (int i = 0; i < adapter.getData().size(); i++) {
                    if (i == position) {
                        cardDTO = (VipCardBean.CardDTO) adapter.getData().get(i);
                        cardDTO.setSelect(true);
                    } else {
                        ((VipCardBean.CardDTO) adapter.getData().get(i)).setSelect(false);
                    }
                }
                vipCardAdapter.notifyDataSetChanged();
            }
        });
        //购买记录
        histotyAdapter = new HistotyAdapter(R.layout.item_pay_record);
        mRvHistoryList.setLayoutManager(new LinearLayoutManager(this));
        mRvHistoryList.setAdapter(histotyAdapter);
    }

    @Override
    public void initData() {
        initUserInfo();
        //获取vip信息
        mPresenter.getVip();
        mPresenter.getMembershipRules();
        mPresenter.getVipCard();
        mPresenter.getHistory("1", "100");
    }

    private void initUserInfo() {
        LoginBean loginBean = ConfigStorage.getInstance().getUserBean();
        if (loginBean != null && loginBean.getUserinfo() != null) {
            mTvName.setText(loginBean.getUserinfo().getNickname());
            mTvPhone.setText(loginBean.getUserinfo().getMobile());
        }
    }

    @Override
    protected void initPresenter() {
        mPresenter = new GetVIPInfoPresenter(this);
    }

    @OnClick({R.id.iv_member_back, R.id.tv_pay})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_member_back:
                finish();
                break;
            case R.id.tv_pay:

                if (vipInfoBean != null && cardDTO != null) {
                    new BottomPayDialog.Builder(this).setData(cardDTO.getMoney()).setCallBack(new BottomPayDialog.Builder.ItemClickListener() {
                        @Override
                        public void onPayClick(int payItem) {
                            orderPay(cardDTO.getId(), payItem);
//                            ToastUtils.showCenterLongToast(payItem == 0 ? "支付宝" : "微信");
                        }
                    }).show();
                }else {
                    ToastUtils.showCenterToast("信息获取失败，请退出页面重试。");
                }


                break;
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_member;
    }

    @Override
    public void onPaySuccess() {
        ToastUtils.showCenterToast("开通成功！");
        mTvVipInfo.setText("尊贵的VIP，您的会员将于" + vipInfoBean.getVip_time() + "到期");
    }

    @Override
    public void VipSuccess(VipInfoBean vipInfo) {
        vipInfoBean = vipInfo;
        if (!TextUtils.isEmpty(vipInfoBean.getVip_time())) {
            mTvVipInfo.setText("尊贵的VIP，您的会员将于" + vipInfoBean.getVip_time() + "到期");
        }
        tvPay.setText(vipInfoBean.getIs_vip() == 1 ? "立即续费" : "立即开通");
    }

    @Override
    public void VipFail(String error) {
        ToastUtils.showCenterToast(error);
    }

    @Override
    public void MembershipRulesSuccess(String membershipRules) {
        mTvVipRole.loadDataWithBaseURL(null, membershipRules, "text/html", "UTF-8", "");
    }

    @Override
    public void MembershipRulesFail(String error) {

    }

    @Override
    public void VipCardSuccess(VipCardBean vipCardBean) {
        List<VipCardBean.CardDTO> list = new ArrayList<>();

        for (VipCardBean.CardDTO dto : vipCardBean.getCard()) {
            if ("安卓".equals(dto.getOs())) {
                list.add(dto);
            }
        }
        if (list.size() > 0) {
            list.get(0).setSelect(true);
            cardDTO = list.get(0);
        }
        vipCardAdapter.setNewData(list);
    }

    @Override
    public void VipCardFail(String error) {

    }

    @Override
    public void HistorySuccess(List<HistoryBean> historyBeans) {
        histotyAdapter.setNewData(historyBeans);
    }

    @Override
    public void HistoryFail(String error) {

    }
}
